package com.imooc.miaosha.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisService {

    @Autowired
    JedisPool jedisPool;

    /**
     * 获取value值
     * @param keyPrefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        //获取Jedis对象
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //获取带有前缀的真正key,前缀+key
            String realKey = keyPrefix.getPrefix() + key;
            String str = jedis.get(realKey);
            return stringToBean(str,clazz);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置key-value值
     * @param keyPrefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix keyPrefix,String key,T value){
        //获取Jedis对象
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            String str = beanToString(value);
            if(str == null || str.length() <= 0){
                return false;
            }

            String realKey = keyPrefix.getPrefix() + key;
            int seconds = keyPrefix.expireSeconds();

            if(seconds <= 0){
                jedis.set(realKey,str);
            }else{
                //设置超时时间
                jedis.setex(realKey,seconds,str);
            }
            return true;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断是否存在key
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> boolean exits(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;

        try{
            jedis = jedisPool.getResource();

            String realKey = keyPrefix.getPrefix() + key;

            return jedis.exists(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 增加值
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long incr(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();

            String realKey = keyPrefix.getPrefix() + key;

            return jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 减少值
     * @param keyPrefix
     * @param key
     * @param <T>
     * @return
     */
    public <T> Long decr(KeyPrefix keyPrefix,String key){
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();

            String realKey = keyPrefix.getPrefix() + key;

            return jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    private void returnToPool(Jedis jedis){
    jedis.close();
}

    @SuppressWarnings("unchecked")
    public static  <T> T stringToBean(String str,Class<T> clazz){
        if(str == null || str.length() <= 0 || clazz == null){
            return null;
        }

        if(clazz == int.class || clazz == Integer.class){
            return (T) Integer.valueOf(str);
        }else if(clazz == long.class || clazz == Long.class){
            return (T) Long.valueOf(str);
        }else if(clazz == String.class){
            return (T) str;
        }else{
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }

    public static  <T> String beanToString(T value){
        if(value == null){
            return null;
        }

        Class<?> clazz = value.getClass();
        if(clazz == int.class || clazz == Integer.class){
            return "" + value;
        }else if(clazz == long.class || clazz == Long.class){
            return "" + value;
        }else if(clazz == String.class){
            return (String)value;
        }else{
            return JSON.toJSONString(value);
        }
    }

    public void delete(KeyPrefix idPrefix, String id) {
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();

            String realKey = idPrefix.getPrefix() + id;

            jedis.del(realKey);
        }finally {
            returnToPool(jedis);
        }
    }
}
